package com.yeyks.common.utils;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableMap;
import com.yeyks.common.param.finance.FinanceBindParam;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @author fuchangshuai
 * @date 2019/5/6 0006
 */
public class JsonUtils {

    // 定义jackson对象
    private static final ObjectMapper MAPPER = new ObjectMapper();

    /**
     * 将对象转换成json字符串。
     *
     * @param data
     * @return
     */
    public static String objectToJson(Object data) {
        try {
            return MAPPER.writeValueAsString(data);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将json结果集转化为对象
     *
     * @param jsonData json数据
     * @param beanType 对象中的object类型
     * @return
     */
    public static <T> T jsonToPojo(String jsonData, Class<T> beanType) {
        try {
            return MAPPER.readValue(jsonData, beanType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将json数据转换成pojo对象list
     *
     * @param jsonData
     * @param beanType
     * @return
     */
    public static <T> List<T> jsonToList(String jsonData, Class<T> beanType) {
        JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
        try {
            return MAPPER.readValue(jsonData, javaType);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 向json中添加一个key和value(对象)
     *
     * @param key     新添加的key
     * @param bean    新添加的bean对象
     * @param jsonStr 原始json
     * @return
     */
    public static <T> String addJsonBean(String key, T bean, String jsonStr) {
        if (null == jsonStr || jsonStr.length() == 0) {
            if (null == key || key.length() == 0 || null == bean) {
                return null;
            }
            return JSONObject.toJSONString(ImmutableMap.of(key, bean));
        }
        if (!JSONUtil.isJson(jsonStr)) {
            return jsonStr;
        }
        try {
            Map<String, Object> extraMap = JSONObject.parseObject(jsonStr);
            extraMap.put(key, bean);
            return JSONObject.toJSONString(extraMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonStr;
    }

    public static <T> String addJsonBean(String key, T bean) {
        return addJsonBean(key, bean, null);
    }

    public static <T extends Collection> String addJsonList(String key, T list) {
        return JSONObject.toJSONString(ImmutableMap.of(key, list));
    }

    /**
     * 从json中解析出来key对应的value
     *
     * @param key     需要解析的key
     * @param jsonStr json
     * @return
     */
    public static String getJsonToStr(String key, String jsonStr) {
        if (null == jsonStr || !JSONUtil.isJson(jsonStr)) {
            return null;
        }
        if (!JSONUtil.isJson(jsonStr)) {
            return null;
        }
        try {
            JSONObject jsonObject = JSONObject.parseObject(jsonStr);
            return jsonObject.getString(key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 从json中解析出来key对应的value并转化成对象
     *
     * @param key     需要解析的key
     * @param jsonStr json
     * @return
     */
    public static <T> T getJsonToBean(String key, String jsonStr, Class<T> beanType) {
        if (null == jsonStr || !JSONUtil.isJson(jsonStr)) {
            return null;
        }
        if (!JSONUtil.isJson(jsonStr)) {
            return null;
        }
        try {
            JSONObject jsonObject = JSONObject.parseObject(jsonStr);
            String value = jsonObject.getString(key);
            if (null == value) {
                return null;
            }
            return jsonToPojo(value, beanType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        String s = addJsonBean("pay_account", "value", "{\"addressDetail\":\"测试运营商3\"}");
        FinanceBindParam financeBindParam = new FinanceBindParam();
        financeBindParam.setBankCard("123456789789798");
        financeBindParam.setBankName("银行名称");
        financeBindParam.setBranchBankName("银行支行");
        financeBindParam.setUserName("名字");
        String ss = addJsonBean("pay_account", financeBindParam, "{\"addressDetail\":\"测试运营商3\"}");
        System.out.println(ss);
    }
}
